<template>
<div class="all">
  <div class="login-container">
    <div class="login-wrap">
      <el-tabs v-model="activeName" @tab-click="handleClick">
      <el-tab-pane label="学生" name="学生"></el-tab-pane>
      <el-tab-pane label="教师" name="教师"></el-tab-pane>
      <el-tab-pane label="管理员" name="管理员"></el-tab-pane>
    </el-tabs>
<div>
  <h2></h2>
</div>
      <el-form
        label-position="left"
        :model="ruleForm"
        :rules="rules"
        ref="ruleForm"
        label-width="0px"
        class="demo-ruleForm"
      >

        <el-form-item prop="userName">
          <el-input type="text" v-model="ruleForm.userName" auto-complete="off" placeholder="账号"></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input type="password" v-model="ruleForm.password" auto-complete="off" placeholder="密码"></el-input>
        </el-form-item>
        <el-form-item style="width:100%;">
          <el-button
            type="primary"
            style="width:100%;"
            @click="submitForm('ruleForm')"
            :loading="logining"
          >登录</el-button>
        </el-form-item>
      </el-form>
  </div>
  </div>
  </div>
</template>

<script type="text/ecmascript-6">
import { login } from "../api/user";
export default {
   
  name: "login",
  data() {
    return {
      //定义loading默认为false
   activeName: '学生',
   logining: false,
      ruleForm: {
        //username和password默认为空
        userName: "manager",
        password: "123456"
      },
      //rules前端验证
      rules: {
        userName: [
          {
            required: true,
            message: "请输⼊账号",
            trigger: "blur"
          }
        ],
        password: [
          {
            required: true,
            message: "请输⼊密码",
            trigger: "blur"
          }
        ]
      }
    };
  },
  // ⾥⾯的函数只有调⽤才会执⾏
  methods: {
     handleClick(tab, event) {
        console.log(tab, event);
        console.log(this.activeName)
      },
    //获取info列表
    submitForm(formName) {
      //表单验证
      this.$refs[formName].validate(valid => {
        if (valid) {
          /*
          //Ajax验证
          login(this.ruleForm).then(response => {
            console.log(response);
            if (response.code === 200) {
              // 缓存User
              localStorage.setItem("user", JSON.stringify(response.data));
              this.$router.push({
                path: "/index"
              });
            } else {
              this.$message.error(response.message);
              this.logining = false;
              return false;
            }
          });*/
          //模拟数据 学生 教师 管理员
            if (this.ruleForm.userName == "teacher" && this.ruleForm.password == '123456') {
                localStorage.setItem("token_key", JSON.stringify(
                  {
                    "userName":this.ruleForm.userName,
                    "userId": "1",
                    "type":"teacher"
                  }
                ));
              this.$router.push({
                path: "/index"
              });
            } else if (this.ruleForm.userName == "student" && this.ruleForm.password == '123456') {
               localStorage.setItem("token_key", JSON.stringify(
                  {
                    "userName":this.ruleForm.userName,
                    "userId": "2",
                    "type":"student"
                  }
                ));
                this.$router.push({
                path: "/index"
              });
            } else if (this.ruleForm.userName == "manager" && this.ruleForm.password == '123456') {
              localStorage.setItem("token_key", JSON.stringify(
                  {
                    "userName":this.ruleForm.userName,
                    "userId": "3",
                    "type":"manager"
                  }
                ));
                this.$router.push({
                path: "/index"
              });
            
            } else {
              this.$message.error("用户名或密码错误！");
              this.logining = false;
              return false;
            } 
        } else {
          this.$message.error("请输⼊⽤⼾名密码！");
          this.logining = false;
          return false;
        }
      });
    }
  }
};
</script>

<style scoped>

.login-container {
  border-radius: 10px;
  margin: 0px auto;
  width: 350px;
  height:250px;
  padding: 20px 35px 15px 35px;
  background: #fff;
  border: 1px solid #eaeaea;
  text-align: left;
  box-shadow: 0 0 20px 2px rgba(0, 0, 0, 0.1);
}

.title {
  margin: 0px auto 40px auto;
  text-align: center;
  color: #505458;
}

.remember {
  margin: 0px 0px 35px 0px;
}

.code-box {
  text-align: right;
}

.codeimg {
  height: 40px;
}
.all{
  padding:10% 0px 0px 0px;
}
.login-wrap{
  margin-top:0px;
}
.demo-ruleForm{
  margin-top:15px;
}

.all{
box-sizing: border-box;
  width: 100%;
  height: 100%;
  padding-top: 10%;

  background-image: url();
  background-repeat: no-repeat;
  background-position: center right;
  background-size: 100%;
}
</style>
